package com.ruoyi.web.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.enums.AbnormalEnum;
import com.ruoyi.framework.web.exception.TadpoleException;
import com.ruoyi.web.constant.WalletConstant;
import com.ruoyi.web.domain.Currency;
import com.ruoyi.web.domain.RechargeRecord;
import com.ruoyi.web.domain.UserCapital;
import com.ruoyi.web.enums.CommonDictEnum;
import com.ruoyi.web.mapper.UserCapitalMapper;
import com.ruoyi.web.service.ICurrencyService;
import com.ruoyi.web.service.IUserCapitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户资金账户Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-30
 */
@Service
public class UserCapitalServiceImpl implements IUserCapitalService {
    @Autowired
    private UserCapitalMapper userCapitalMapper;

    @Autowired
    private ICurrencyService currencyService;

    @Override
    public R<BigDecimal> getUserBalance() {
        try {
            Long userId = SecurityUtils.getUserId();
            List<UserCapital> list = userCapitalMapper.selectUserCapitalList(new UserCapital() {{
                setUserId(userId);
            }});
            UserCapital capital = list.size() > 0 ? list.get(0) : null;
            if (capital == null) {
                throw new TadpoleException(AbnormalEnum.MSG.OPTIONS_USER_NOT_FOUND.getCode(), "用户未开通资金账户");
            }
            return R.ok(capital.getBalance());
        } catch (TadpoleException exception) {
            throw exception;
        } catch (Exception e) {
            throw new TadpoleException(AbnormalEnum.MSG.DISCONNECTED);
        }
    }

    /**
     * 根据用户id查询
     *
     * @param userId 用户id
     * @return {@link UserCapital}
     * @since 2023/3/27 17:39
     */
    @Override
    public UserCapital selectByUserId(Long userId) {
        try {
            List<UserCapital> list = userCapitalMapper.selectUserCapitalList(new UserCapital() {{
                setUserId(userId);
            }});
            UserCapital capital = list.size() > 0 ? list.get(0) : null;
            if (capital == null) {
                throw new TadpoleException(AbnormalEnum.MSG.OPTIONS_USER_NOT_FOUND.getCode(), "用户未开通资金账户");
            }
            return capital;
        } catch (TadpoleException exception) {
            throw exception;
        } catch (Exception e) {
            throw new TadpoleException(AbnormalEnum.MSG.DISCONNECTED);
        }
    }

    @Override
    public void rechargeThen(RechargeRecord record) {
        try {
            // 获取币对信息
            List<Currency> currencies = currencyService.selectCurrencyList(new Currency() {{
                setName(WalletConstant.USDT);
                setPublicChain(record.getPublicChain());
                setState(CommonDictEnum.Status.NORMAL.getValue());
            }});
            Currency currencyServiceOne = currencies.size() > 0 ? currencies.get(0) : null;
            if (currencyServiceOne == null) {
                return;
            }
            // 获取资金账户
            List<UserCapital> capitals = userCapitalMapper.selectUserCapitalList(new UserCapital() {{
                setUserId(record.getUserId());
            }});
            UserCapital capitalAccount = capitals.size() > 0 ? capitals.get(0) : null;
            if (capitalAccount != null) {
                // 本次充值数量 (扣除手续费 - 计算精度)
                BigDecimal bigDecimal = record.getQuantity().setScale(record.getQuantity().scale(), RoundingMode.DOWN);
                // 修改资金账户余额
                capitalAccount.setBalance(capitalAccount.getBalance().add(bigDecimal));
                userCapitalMapper.updateUserCapital(capitalAccount);
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 查询用户资金账户
     *
     * @param id 用户资金账户主键
     * @return 用户资金账户
     */
    @Override
    public UserCapital selectUserCapitalById(Long id) {
        return userCapitalMapper.selectUserCapitalById(id);
    }

    /**
     * 查询用户资金账户列表
     *
     * @param userCapital 用户资金账户
     * @return 用户资金账户
     */
    @Override
    public List<UserCapital> selectUserCapitalList(UserCapital userCapital) {
        return userCapitalMapper.selectUserCapitalList(userCapital);
    }

    /**
     * 新增用户资金账户
     *
     * @param userCapital 用户资金账户
     * @return 结果
     */
    @Override
    public int insertUserCapital(UserCapital userCapital) {
        return userCapitalMapper.insertUserCapital(userCapital);
    }

    /**
     * 修改用户资金账户
     *
     * @param userCapital 用户资金账户
     * @return 结果
     */
    @Override
    public int updateUserCapital(UserCapital userCapital) {
        return userCapitalMapper.updateUserCapital(userCapital);
    }

    /**
     * 批量删除用户资金账户
     *
     * @param ids 需要删除的用户资金账户主键
     * @return 结果
     */
    @Override
    public int deleteUserCapitalByIds(Long[] ids) {
        return userCapitalMapper.deleteUserCapitalByIds(ids);
    }

    /**
     * 删除用户资金账户信息
     *
     * @param id 用户资金账户主键
     * @return 结果
     */
    @Override
    public int deleteUserCapitalById(Long id) {
        return userCapitalMapper.deleteUserCapitalById(id);
    }

    /**
     * 根据userIdList查询
     *
     * @param userIdList 用户idList
     * @param condition  查询条件
     * @return {@link List< UserCapital>}
     * @since 2023/4/4 12:50
     */
    @Override
    public List<UserCapital> listByUserIdList(List<Long> userIdList, UserCapital condition) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return new ArrayList<>(0);
        }

        return userCapitalMapper.listByUserIdList(userIdList, condition);
    }
}
